﻿using DFGK.IService;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.物料管理
{
    /// <summary>
    /// 物料批次
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class MaterialKeyController : BaseController
    {
        private readonly IMaterialKeyService materialKeyService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="materialKeyService"></param>
        public MaterialKeyController(IWebHostEnvironment environment, IMaterialKeyService materialKeyService) : base(environment)
        {
            this.materialKeyService = materialKeyService;
        }

        /// <summary>
        /// 添加物料批次
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMaterialKey(MaterialKey m)
        {
            if (string.IsNullOrEmpty(m.MaterialCode))
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            bool IsOK = materialKeyService.AddMaterialKey(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！批次已存在！", "500"));
        }

        /// <summary>
        /// 删除物料批次
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelMaterialKey(long mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = materialKeyService.DelMaterialKey(mid);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));

        }

        /// <summary>
        /// 修改批次
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdMaterialKey(MaterialKey m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(m.MaterialCode))
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            bool IsOK = materialKeyService.UpdMaterialKey(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！编码已存在！", "500"));

        }

        /// <summary>
        /// 根据主键获取对象
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMaterialKeyById(long mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var rule = materialKeyService.GetMaterialKeyById(mid);
            if (rule != null)
            {
                return Ok(BaseResult.Result(rule, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！Id可能有误或已被删除！", "500"));

        }

        /// <summary>
        /// 显示物料批次列表
        /// </summary>
        /// <param name="mname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMaterialKeyList(string? mname, int pageIndex = 1, int pageSize = 10)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = materialKeyService.GetMaterialKeyList(mname, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));

        }
    }
}
